<script setup>
import PageContainer from '@/components/PageContainer.vue'
import { ref } from 'vue'
import { artGetChannelsService, artDelChannelsService } from '@/api/article.js'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from '@/views/article/components/ChannelEdit.vue'

// 表数据
const channelList = ref([])
// 数据表格转圈转圈加载中....
const loading = ref(false)
// 引用自定义的弹窗组件 ChannelEdit
const dialog = ref()

const getChannelList = async () => {
  loading.value = true
  const res = await artGetChannelsService()
  channelList.value = res.data.data
  loading.value = false
}

// const localRes = {
//   'code': 0,
//   'message': '获取文章分类列表成功！',
//   'data': [
//     {
//       'id': 1070,
//       'cate_name': '科技',
//       'cate_alias': 'keji',
//     },
//     {
//       'id': 1071,
//       'cate_name': '历史',
//       'cate_alias': 'lishi',
//     },
//     {
//       'id': 1071,
//       'cate_name': '历史',
//       'cate_alias': 'lishi',
//     },
//     {
//       'id': 1071,
//       'cate_name': '历史',
//       'cate_alias': 'lishi',
//     },
//   ],
// }
// channelList.value = localRes.data

getChannelList()

const onEditChannel = (row) => {
  dialog.value.open(row)
}
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确认要删除该分类吗', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消',
  })
  await artDelChannelsService(row.id)
  ElMessage.success('删除成功')
  getChannelList()
}
const onAddChannel = () => {
  dialog.value.open({})
}

const onSuccess = () => {
  getChannelList()
}


</script>

<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button type="primary" @click="onAddChannel">添加分类</el-button>
    </template>
    <el-table v-loading="loading" :data="channelList" size="large">
      <el-table-column type="index" label="序号" min-width="70"></el-table-column>
      <el-table-column prop="cate_name" label="分类名称" min-width="100"></el-table-column>
      <el-table-column prop="cate_alias" label="分类别名" min-width="100"></el-table-column>
      <el-table-column label="操作" min-width="200">
        <template #default="{row}">
          <el-button :icon="Edit" type="primary" plain size="small" @click="onEditChannel(row)">编辑</el-button>
          <el-button :icon="Delete" type="danger" plain size="small" @click="onDelChannel(row)">删除</el-button>
        </template>
      </el-table-column>
      <!--加载不到数据的插槽-->
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <ChannelEdit ref="dialog" @success="onSuccess"></ChannelEdit>
  </page-container>
</template>

<style lang="scss" scoped>

</style>